<?php
namespace app\api\controller;

class ReturnMoney extends Permission
{
    public function getList()
    {
        $page = isset($this->input_data['page']) ? $this->input_data['page'] : 1;
        $limit = isset($this->input_data['limit']) ? $this->input_data['limit'] : 15;
        $map = [];

        if( isset($this->input_data['area']) && $this->input_data['area'] != '') $map['b.area'] = ['LIKE','%'.$this->input_data['area'].'%'];
        if( isset($this->input_data['company']) && $this->input_data['company'] != '') $map['b.company'] = ['LIKE','%'.$this->input_data['company'].'%'];
        if( isset($this->input_data['plate_number']) && $this->input_data['plate_number'] != '') $map['b.plate_number'] = ['LIKE','%'.$this->input_data['plate_number'].'%'];

        if( isset($this->input_data['status']) && $this->input_data['status'] != 99999999 ) $map['a.status'] = $this->input_data['status'];


        $total = db('ReturnMoney')->alias('a')->join('vehicle b','a.vehicle_id = b.id','LEFT')->where($map)->count();
        $rows = db('ReturnMoney')->alias('a')->join('vehicle b','a.vehicle_id = b.id','LEFT')->field('a.*,b.area,b.company,b.plate_number,b.realname,b.tel')->order('id', 'desc')->page($page,$limit)->where($map)->select();


        $list = [];
        foreach ($rows as $v) {
            $v['amount'] = formatMoneyShow($v['amount']);
            $list[] = $v;
        }
        
        $data = [
            'total' => $total,
            'list' => $list
        ];
        $this->resSuc($data);
    }

    // 还款
    public function return()
    {
        
        $amount = formatMoneySave($this->input_data['amount']);
        # 保存支付记录
        $data = [
            'cate' => '多收返还',
            'type' => '支出',
            'relation_id' => $this->input_data['id'],
            'relation_name' => $this->input_data['plate_number'],
            'amount' => $amount * -1,
            'images' => $this->input_data['images'],
            'create_user' => $this->user['realname'],
            'create_user_id' => $this->user['id'],
            'create_time' => time()
        ];
        db('PayRecord')->insertGetId($data);

        # 更新待返还列表
        $map = [
            'id' => $this->input_data['id']
        ];
        $data = [
            'status' => 1,
            'return_user' => $this->user['realname'],
            'return_user_id' => $this->user['id'],
            'return_time' => time(),
        ];
        db('ReturnMoney')->where($map)->update($data);
            
        
        

        $this->resSuc('ok');
    }

    // 提前还款
    private function _prepay($ReturnMoneyId)
    {
        $map = [
            'relation_id' => $ReturnMoneyId,
            'cate' => '账单还款'
        ];
        $amount = db('PayRecord')->where($map)->sum('amount');
        $data = [
            'return_amount' => $amount, //已还金额
            'preturn_images' => $this->input_data['preturn_images'],
            'return_type' => $this->input_data['type'] == '正常' ? '' : $this->input_data['type'],
            'deduction_amount' => formatMoneySave($this->input_data['deduction_amount']),
            'status' => 1
        ];

        db('ReturnMoney')->where(['id'=>$ReturnMoneyId])->update($data);
    }

    // 还款数据验证
    private function _validRepay()
    {
        if($this->input_data['amount'] == 0) $this->resErr('还款金额不能为0');
        if($this->input_data['images'] == '') $this->resEr('请上传付款凭证');

        if($this->input_data['type'] == '提前还款')
        {
            if($this->input_data['deduction_amount'] < 1 ) $this->resErr('减免金额不能为0');
            if($this->input_data['preturn_images'] == '') $this->resEr('请上传提前还款相关材料');
        }
    }

    // 更新账单已还总数
    private function _updateReturnMoneyRepay($ReturnMoneyId)
    {
        $ReturnMoney = db('ReturnMoney')->where(['id'=>$ReturnMoneyId])->find();
        $totalAmount = bcadd($ReturnMoney['amount'], $ReturnMoney['interest']);
        $map = [
            'relation_id' => $ReturnMoneyId,
            'cate' => '账单还款'
        ];
        $repayAmount = db('PayRecord')->where($map)->sum('amount');
        $data = [
            'return_amount' => $repayAmount,
            'status' => $totalAmount > $repayAmount ? 0 : 1,
        ];

        db('ReturnMoney')->where(['id'=>$ReturnMoneyId])->update($data);
    }

    private function _repayReturnMoney($ReturnMoneyId,$amount)
    {
        # 找未还清的账单
        $map = [
            'ReturnMoney_id' => $ReturnMoneyId,
            'return_done' => 0
        ];
        $details = db('ReturnMoneyDetail')->where($map)->order('id ASC')->select();

        # 还进去
        foreach ($details as $v) {
            if( $amount < 1 ) break;
            // 当期账单剩余未还金额
            $sy = bcsub($v['amount_total'],$v['return_total']);
            $repayAmount = $amount > $sy ? $sy : $amount;
            $v['return_total'] += $repayAmount;
            $v['return_done'] = $v['return_total'] == $v['amount_total'] ? 1 : 0;
            db('ReturnMoneyDetail')->update($v);
            $amount -= $repayAmount;
        }
    }
  

}
